﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.Threading;

namespace SolutionFramework.Tests
{
    public partial class SilverlightHost : Form
    {
        private ServiceHost host;
        private bool running;

        public SilverlightHost()
        {
            Form.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        public string Title
        {
            get
            {
                return this.Text;
            }

            set
            {
                this.Text = value;
            }
        }

        public void Navigate(string url)
        {
            WebBrowser.Navigate(url);
        }

        public void RunTest(string testName)
        {
        }

        public void Shutdown()
        {
            this.Close();
        }

        private void WebBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            StatusLabel.Text = "Waiting for " + e.Url;
        }

        private void WebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            StatusLabel.Text = "Loading " + e.Url;
        }

        private void WebBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            ProgressBar.Maximum = (int) e.MaximumProgress;
            ProgressBar.Value = (int) e.CurrentProgress;
        }

        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            StatusLabel.Text = "Complete";
        }

        private void SilverlightHost_Load(object sender, EventArgs e)
        {
            var thread = new Thread(HostThread);

            thread.Start();
        }

        private void HostThread()
        {
            running = true;

            var service = new SLHostService(this);
            host = new ServiceHost(service);

            host.Open();

            while (running)
            {
                Thread.Sleep(100);
            }
        }

        private void SilverlightHost_FormClosing(object sender, FormClosingEventArgs e)
        {
            running = false;
        }
    }
}
